package org.oschina.api.user.role.controller;

import java.util.List;
import org.apache.commons.lang.ArrayUtils;
import org.oschina.api.BaseController;
import org.oschina.api.manage.module.vo.ModuleVo;
import org.oschina.api.user.role.model.UserRoleType;
import org.oschina.api.user.role.service.UserRoleService;
import org.oschina.api.user.role.service.UserRoleTypeService;
import org.oschina.api.user.role.vo.UserRoleTypeVo;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.handler.validator.filter.TextOnlyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=Config.API_PATH+"/user/role")
public class UserRoleController extends BaseController {

	@Autowired
	private UserRoleService roleService;
	@Autowired
	private UserRoleTypeService roleTypeService;
	
	/**
	 * 更新
	 * @param roleType
	 * @return
	 */
	@Validate
	@RequestMapping(value="/type/update/{id}",method=RequestMethod.POST)
	public Result update
	(
		@Validate(validateField=
		{
			@ValidateField(name="id",validateValue=true,min=1),
			@ValidateField(name="name",filter={TextOnlyFilter.class})
		}) UserRoleType roleType
	)
	{
		return roleTypeService.update(roleType);
	}
	
	/**
	 * 删除
	 * @param id
	 * @param target
	 * @return
	 */
	@Validate
	@RequestMapping(value="/type/delete/{id}",method=RequestMethod.POST)
	public Result update
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id,
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer target
	)
	{
		return roleTypeService.delete(id, target);
	}
	
	/**
	 * 创建角色
	 * @param roleType
	 * @return
	 */
	@Validate
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public Result create
	(
		@Validate(validateField=@ValidateField(name="name",validateValue=true,filter={TextOnlyFilter.class})) UserRoleType roleType
	)
	{
		return roleTypeService.create(roleType);
	}
	
	/**
	 * 设置默认
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/default/update",method=RequestMethod.POST)
	public Result setDefault
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return roleTypeService.setDefault(id);
	}
	
	/**
	 * 根据角色和类型返回
	 * @param role
	 * @param type
	 * @return
	 */
	@Validate
	@RequestMapping(value="/{role}/type/{type}",method=RequestMethod.GET)
	public List<ModuleVo> findByRoleAndType
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer role,
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer type
	)
	{
		return roleService.findVoByRoleAndType(role, type);
	}
	
	/**
	 * 返回所有用户类型
	 * @return
	 */
	@RequestMapping(value="/type",method=RequestMethod.GET)
	public List<UserRoleTypeVo> findType()
	{
		return roleTypeService.findVo();
	}
	
	/**
	 * 更新角色模块
	 * @param role
	 * @param modules
	 * @return
	 */
	@Validate
	@RequestMapping(value="/{role}/module/update",method=RequestMethod.POST)
	public Result updateModuleByRole
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer type,
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer role,
		Integer modules[]
	)
	{
		if(modules==null || modules.length==0)
			modules=new Integer[0];
		
		return roleService.updateModuleByRole(role, type ,ArrayUtils.toPrimitive(modules));
	}
}